package org.cuppaper.command;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.cuppaper.aide.Aide;
import org.cuppaper.utils.CleanItem;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class AideCommand implements TabExecutor {
    private final Aide plugin;
    
    public AideCommand(Aide plugin) {
        this.plugin = plugin;
    }
    
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if(label.equalsIgnoreCase("aide")) {
            if(sender instanceof Player) {
                if (args.length == 0) {
                    sender.sendMessage(
                            ChatColor.AQUA + "========== Aide辅助插件指令帮助列表 ==========\n" +
                                    ChatColor.GOLD + "掉落物类：\n" +
                                    ChatColor.YELLOW + "/aide searchitem" + ChatColor.AQUA + " -搜索自己5格范围内的掉落物\n" +
                                    ChatColor.YELLOW + "/aide cleanallitem" + ChatColor.AQUA + " -清除自己5格范围内的掉落物\n" +
                                    ChatColor.YELLOW + "/aide cleanitem [掉落物名字]" + ChatColor.AQUA + " -清除自己5格范围内的所有指定掉落物\n" +
                                    ChatColor.YELLOW + "/aide cleanitem [掉落物名字] [数量]" + ChatColor.AQUA + " -清除自己5格范围内的指定数量的指定掉落物\n" +
                                    ChatColor.GOLD + "辅助功能类：\n" +
                                    ChatColor.YELLOW + "/aide reload" + ChatColor.AQUA + " -重新加载插件配置\n" +
                                    ChatColor.AQUA + "空手鼠标右击蜂巢或者蜂箱可以查看蜜蜂数量和蜂蜜量\n" +
                                    ChatColor.AQUA +"========================================");
                    return true;
                } else {
                    if (args[0].equalsIgnoreCase("searchitem")) {
                        Player player = (Player) sender;
                        if (sender.hasPermission("aide.player.searchitem")) {
                            var ItemCount = CleanItem.getNearbyItems(player, 5);
                            for (String message : ItemCount) {
                                player.sendMessage(message);
                            }
                        } else {
                            sender.sendMessage(ChatColor.RED + "你需要 " + ChatColor.YELLOW + "aide.player.searchitem" + ChatColor.RED + " 以执行此命令实现 " + ChatColor.GOLD + "搜索范围内掉落物" + ChatColor.RED + " 的功能");
                        }
                    } else if (args[0].equalsIgnoreCase("cleanallitem")) {
                        Player player = (Player) sender;
                        if (sender.hasPermission("aide.player.cleanitem")) {
                            int removed = CleanItem.cleanNearbyItems(player, 5, null, -1);
                            player.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.AQUA + "成功清除了 " + ChatColor.YELLOW + removed + ChatColor.AQUA + " 个掉落物");
                        } else {
                            sender.sendMessage(ChatColor.RED + "你需要 " + ChatColor.YELLOW + "aide.player.cleanitem" + ChatColor.RED + " 权限 以执行此命令实现 " + ChatColor.GOLD + "清除范围内掉落物" + ChatColor.RED + " 的功能");
                        }
                    } else if (args[0].equalsIgnoreCase("cleanitem")) {
                        Player player = (Player) sender;
                        if (sender.hasPermission("aide.player.cleanitem")) {
                            if (args.length == 1) {
                                player.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.RED + "请指定要删除的物品名称");
                                return true;
                            } else if (args.length == 2) {
                                String itemName = args[1];
                                int removed = CleanItem.cleanNearbyItems(player, 5, itemName, -1);
                                player.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.AQUA + "成功清除了 " + ChatColor.YELLOW + removed + ChatColor.AQUA + " 个 " + ChatColor.GOLD + itemName);
                            } else {
                                String itemName = args[1];
                                try {
                                    int amount = Integer.parseInt(args[2]);
                                    if (amount <= 0) {
                                        player.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.RED + "数量必须大于 0");
                                        return true;
                                    }
                                    int removed = CleanItem.cleanNearbyItems(player, 5, itemName, amount);
                                    player.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.AQUA + "成功清除了 " + ChatColor.YELLOW + removed + ChatColor.AQUA + " 个 " + ChatColor.GOLD + itemName);
                                } catch (NumberFormatException e) {
                                    player.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.RED + "无效的数量：" + ChatColor.DARK_RED + args[2]);
                                }
                            }
                        } else {
                            sender.sendMessage(ChatColor.RED + "你需要 " + ChatColor.YELLOW + "aide.player.cleanitem" + ChatColor.RED + " 权限 以执行此命令实现 " + ChatColor.GOLD + "清除范围内掉落物" + ChatColor.RED + " 的功能");
                        }
                    } else if (args[0].equalsIgnoreCase("reload")) {
                        if (sender.hasPermission("aide.admin.reload")) {
                            plugin.reloadPlugin();
                            sender.sendMessage(ChatColor.GOLD + "[Aide] " + ChatColor.AQUA + "插件已成功重载！");
                        } else {
                            sender.sendMessage(ChatColor.DARK_RED + "你没有权限执行此命令！");
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "请输入正确的指令!");
                    }
                }
            }
        }
        return true;
    }
    
    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
        if (alias.equalsIgnoreCase("aide")) {
            if (args.length == 1) {
                List<String> suggestions = new ArrayList<>();
                if (sender.hasPermission("aide.player.searchitem")) {
                    suggestions.add("searchitem");
                }
                if (sender.hasPermission("aide.player.cleanitem")) {
                    suggestions.add("cleanitem");
                    suggestions.add("cleanallitem");
                }
                if (sender.hasPermission("aide.admin.reload")) {
                    suggestions.add("reload");
                }
                return suggestions;
            }
        }
        return new ArrayList<>();
    }
}
